﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;

namespace SMS.dao
{
    public class CourseDao
    {
        private static readonly CourseDao INSTANCE = new CourseDao();

        public static CourseDao Instance
        {
            get
            {
                return INSTANCE;
            }
        }

        private CourseDao() { }
		 //long
		public int insert(String name)
        {
            String sql = "insert into course(cor_name) values (@name)";
            DataFetcher.Instance.setCommand(sql, 1);
            DataFetcher.Instance.addParameter(new SqlParameter("@name", name));
            return DataFetcher.Instance.execute();
        }

        public int update(String name, int courseId)
        {
            String sql = "update course set cor_name = @name where cor_id = @courseId";
            DataFetcher.Instance.setCommand(sql, 1);
            DataFetcher.Instance.addParameter(new SqlParameter("@name", name));
            DataFetcher.Instance.addParameter(new SqlParameter("@courseId", courseId));
            return DataFetcher.Instance.execute();
        }

        public int delete(int courseId)
        {
            String sql = "delete from course where cor_id = @courseId";
            DataFetcher.Instance.setCommand(sql, 1);
            DataFetcher.Instance.addParameter(new SqlParameter("@courseId", courseId));
            return DataFetcher.Instance.execute();
        }

        public DataTable findAll()
        {
            String sql = "select cor_id, cor_name from course " +
                         "order by cor_name";
            DataFetcher.Instance.setCommand(sql, 1);
            return DataFetcher.Instance.fetch();
        }

        public ArrayList findById(int id)
        {
            String sql = "select cor_id, cor_name from course where cor_id = @id";
            DataFetcher.Instance.setCommand(sql, 1);
            DataFetcher.Instance.addParameter(new SqlParameter("@id", id));
            return DataFetcher.Instance.getOneRow();
        }

		public DataTable findcourseName(string courseName)
        {
            string sql = "select cor_name from course where cor_name = @cor_name";
            DataFetcher.Instance.setCommand(sql, 1);
            DataFetcher.Instance.addParameter(new SqlParameter("@cor_name", courseName));
            return DataFetcher.Instance.fetch();
        }
        //end of long
    }
}
